package com.zzld.modules.autoinn.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.modules.autoinn.entity.Collar;
import com.zzld.modules.autoinn.service.ICollarService;
import com.zzld.modules.common.dto.CommonJSON;


@Controller
@RequestMapping("/autoinn/collar/")
public class CollarController {

	private String li = "modules/autoinn/collar/collarlist";
	
	@Autowired
	private ICollarService collarService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String lis(){
		return li;
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public CommonJSON list(Model model,HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("nickname", request.getParameter("nickname"));
		map.put("couponZhi", request.getParameter("couponZhi"));
		map.put("collarTime", request.getParameter("collarTime"));
		map.put("state", request.getParameter("state"));
		map.put("starinnertime", request.getAttribute("starinnertime"));
		map.put("endinnertime", request.getAttribute("endinnertime"));
		
		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));
		
		CommonJSON json = new CommonJSON();
		PageInfo pageinfo =   collarService.list(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		
		return json;
	}
	
	
	@RequestMapping(value="delete/{id}",method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model,HttpServletRequest request,
			@PathVariable("id") String id){
		CommonJSON json = new CommonJSON();
		try{
			if(collarService.deleteByPrimaryKey(id)>0){
				json.setMessage(MessageUtils.DELETE_SUCCESS);
				json.setSuccess(true);
			}else{
				json.setMessage(MessageUtils.DELETE_WARNING);
				json.setSuccess(false);
			}
		}catch(Exception e){
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}
	
	

}
